$(function() {
	querydh();
	$('#list').hide();
	
	$('#hydfile').on('change',function(evt) {
		var fileNames = '';
		$.each(this.files, function() {
			fileNames += '<span class="am-badge">' + this.name
					+ '</span> ';
		});
		var filepath = $("input[name='hydfile']").val();
		var extStart = filepath.lastIndexOf(".");
		var ext = filepath.substring(extStart, filepath.length)
				.toUpperCase();
		if (ext != ".BMP" && ext != ".PNG" && ext != ".GIF"
				&& ext != ".JPG" && ext != ".JPEG"
				&& ext != ".PDF") {
			alert("图片限于bmp,png,gif,jpeg,jpg格式");
			return false;
		}
		$('#hydfile-list').html(fileNames);
		
		var files = evt.target.files; 

        for (var i = 0, f; f = files[i]; i++) {

          if (!f.type.match('image.*')) {
            continue;
          }

          var reader = new FileReader();

          reader.onload = (function(theFile) {
            return function(e) {                                
             $("#list img").attr("src",e.target.result);  //预览图片的位置                 
            };
          })(f);

          reader.readAsDataURL(f);
        }
        $('#list').show();
		return true;
	});
	
	$('#my-table').dataTable().fnDestroy();
	oTable = $('#my-table').DataTable({
		"processing" : true, //加载数据时显示正在加载信息   
		"serverSide" : true, //指定从服务器端获取数据   
		"searching" : false,
		"lengthChange" : false, //用户不可改变每页显示数量   
		"ordering" : false,
		"aLengthMenu" : [ 10 ],//设置一页展示10条记录

		"ajax" : {
			"url" : "/btsb/hggllist",
			"dataType" : "json",
			"type" : "post",
			"dataSrc" : "rowsData",
			"data" : function(d) {
				//d.tdh = $('#hwydh').val();
			}
		},
		"columns" : [{
			"data" : null
		}, {
			"data" : "hgsbdh"
		}, {
			"data" : "fjdz"
		}, {
			"data" : "cjsj"
		}, {
			"data" : "gxsj"
		} ],
		"columnDefs" : [{
			"targets" : 0,
			"width" : "5%",
			"render" : function(data, type, row, meta) {
				// 显示行号  
                var startIndex = meta.settings._iDisplayStart;  
                return startIndex + meta.row + 1;  
			}
		}, {
			"targets" : 1,
			"width" : "8%",
			"render" : function(data, type, row, meta) {
				return data;
			}
		}, {
			"targets" : 2,
			"width" : "10%",
			"render" : function(data, type, row, meta) {
				return (!data) ? "暂无" : "<a target='_blank' href='"+data+"'>"+data+"</a>";
			}
		}, {
			"targets" : 3,
			"width" : "10%",
			"render" : function(data, type, row, meta) {
				return date2String(data);
			}
		},{
			"targets" : 4,
			"width" : "10%",
			"render" : function(data, type, row, meta) {	
				return date2String(data);
			}
		}]
	});
})

function savedata() {
	var ysht = $('#hydfile').val();

	if (ysht == "" || ysht == null) {
		alert("请选择上传的文件");
		return;
	}

	var myform = $('#my-form').serialize();
	var url = "/btsb/edithgd?" + myform;
	
	$.ajaxFileUpload({
		url : url,
		secureuri : false,
		fileElementId : 'hydfile',
		dataType : 'text',
		success : function(data) {
			if(data!=null){
				var status = data.substring(data.indexOf("message")+10,data.indexOf('"}'));
				if(status=="Y"){
					alert("保存成功");
					window.location.href="/btsb/addhgglindex";
				}
				else{
					console.log("保存失败");
				}
			}
		},
		error : function(data) {
			console.log(data);
		}
	});
}

//初始化创建人查询
function querydh() {
	$("#hgsbdh").autocomplete(mainDomain+"/btsb/querydh.action", {
		max : 10,
		minChars : 1,
		matchCase : false,
		matchContains : true,
		matchContains : "word",
		scroll : false,
		autoFill : false,
		extraParams : {
			hgdhmh : function() {
				return $("#hgsbdh").val();
			}
		},
		parse : function(data) {
			var rows = [];
			if (!$.isEmptyObject(data.data)) {
				var operatorList = data.data.rows;
				for(var i = 0;i < operatorList.length;i++) {
					rows[rows.length] = {
							data : operatorList[i],
							value : operatorList[i].hgsbdh,
							result : operatorList[i].hgsbdh
					};
				}
			} else {
				$("#hgsbdh").val('');
			}
			return rows;
		},
		formatItem : function(row, i, max) {
			return   row.hgsbdh;
		},
		formatMatch : function(row, i, max) {
			return row.hgsbdh;
		},
		formatResult : function(row) {
			return row.hgsbdh;
		}
	}).result(function(event, row, formatted) {
//		$("#createById").val(row.id);
	});
}